<template>
  <div style="padding:20px;">
    <div class="block" style="display:flex;justify-content:space-between">
      <div class="demonstration">
        <el-button @click="print" type="primary" size="mini" style="margin:30px auto;">打印</el-button>
        <el-button type="primary" size="mini" style="margin:30px auto;">导出</el-button>
        <el-button type="primary" size="mini" style="margin:30px auto;">历史数据</el-button>
      </div>
      <div class="demonstration;" style="align:right">{{ nowdata }}</div>
    </div>
    <div style="margin-top:20px"></div>
    <el-table ref="print" :data="liebiao" border style="width: 100%; :align" v-loading="loading">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column align="center" label="客户名称" prop="company"></el-table-column>
      <!-- <el-table-column
        align="center"
        label="分公司名称"
        prop="branch"
      ></el-table-column>-->
      <!-- <el-table-column align="center" label="部门id" width="180" prop="did"></el-table-column> -->
      <el-table-column align="center" label="部门名称" prop="department"></el-table-column>
      <el-table-column align="center" label="项目名称" prop="proName"></el-table-column>

      <el-table-column align="center" label="项目类型" prop="proType"></el-table-column>
      <el-table-column align="center" label="项目金额" prop="proMoney"></el-table-column>
      <!-- <el-table-column align="center" label="proPrincipal" width="180" prop="proPrincipal"></el-table-column> -->
      <el-table-column align="center" label="项目负责人" prop="proPrincipalName"></el-table-column>
      <!-- <el-table-column
        align="center"
        label="项目公司"
        prop="proCompany"
      ></el-table-column>-->
      <!-- <el-table-column align="center" label="项目部门" width="180" prop="proDepartment"></el-table-column> -->
      <!-- <el-table-column
        align="center"
        label="合同id"
        width="180"
        prop="contractId"
      ></el-table-column>-->
      <el-table-column align="center" label="状态" prop="isFinishName">
        <template slot-scope="scope">
          <el-button size="mini" @click="ontap(scope.$index, scope.row)">{{scope.row.isFinishName}}</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="ratio" width="180" prop="ratio"></el-table-column> -->
      <el-table-column align="center" label="创建时间" prop="createTime" width="180px"></el-table-column>
      <!-- <el-table-column
        align="center"
        label="提交时间"
        prop="submitTime"
      ></el-table-column>-->

      <el-table-column v-if="noprint" align="center" label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--编辑页面 -->
    <!-- 我是编辑的表单页面 bianjixianshi -->
    <!-- :rules="rules" -->
    <el-dialog  title="编辑信息列表" :visible.sync="aa">
      <el-form :model="ruleForm" ref="ruleForm">
        <el-form-item label="客户名称" prop="company" width:180px>
          <el-input v-model="ruleForm.company" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="department" width:180px>
          <el-input v-model="ruleForm.department" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目名称" prop="proName" width:180px>
          <el-input v-model="ruleForm.proName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目类型" prop="proType" width:180px>
          <el-input v-model="ruleForm.proType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目金额" prop="proMoney" width:180px>
          <el-input v-model="ruleForm.proMoney" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目负责人" prop="proPrincipalName" width:180px>
          <el-input v-model="ruleForm.proPrincipalName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目单位" prop="proCompany" width:180px>
          <el-input v-model="ruleForm.proCompany" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="项目部门" prop="proDepartment" width:180px>
          <el-input v-model="ruleForm.proDepartment" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="isFinishName" width:180px>
          <el-input v-model="ruleForm.isFinishName" autocomplete="off" readonly></el-input>
        </el-form-item>

        <el-form-item label="合同参与人员" prop="personList">
          <el-col :span="16">
            <el-select style="width:100% ;" multiple filterable v-model="arr" placeholder="请选择参与人员">
              <el-option
                v-for="(item ,index) in ruleForm.personList"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item label="创建日期" prop="createTime" width:180px>
          <el-input v-model="ruleForm.createTime" autocomplete="off" readonly></el-input>
        </el-form-item>
        <el-form-item label="报送日期" prop="submitTime" width:180px>
          <el-input v-model="ruleForm.submitTime" autocomplete="off" readonly></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>

    <v-pagination
      style="margin-top:20px"
      @page-change="pageChange"
      @page-size-change="pageSizeChange"
      :total="total"
      :page-size="pageSize"
      :layout="['total', 'prev', 'pager', 'next', 'sizer', 'jumper']"
    ></v-pagination>
  </div>
</template>

<!-- 引入组件库 -->

<script>
import { hqfgs } from "@/api/zuzhijiagou";
import { getToken, gettype, getid } from "@/utils/auth";
import request from "@/utils/request";
import axios from "axios";
import { ymd, ym } from "@/api/date";
export default {
  name: "DragTable",

  data() {
    return {
      // 项目列表的内容
      loading: false,
      arr: [],
      show: false,
      ruleForm: [],
      noprint: true,
      liebiao: [],
      loading: false,
      soltid: "",
      pm: {
        cid: "",
        page: 1,
        pageSize: 10
      },
      // 显示编辑页面
      aa: false,

      pageIndex: 1,
      pageSize: 10,
      total: 1,
      nowdata: ymd()
    };
  },
  created() {
    var id = getid();
    this.pm.cid = id;
    console.log("这是");
    console.log(id);
    this.findList();
  },
  methods: {
    ontap(index, row) {
      console.log(index, row.id);
      var path, q;
      if (row.isFinish == 0) {
        //未完成
        path = "firstDraft";
      } else if (row.isFinish == 1) {
        //初稿
        path = "firstCheck";
      } else if (row.isFinish == 2) {
        //一审
        path = "secondCheck";
      } else if (row.isFinish == 3) {
        //二审
        path = "endDraft";
      } else if (row.isFinish == 4) {
        //终稿
        path = "finish";
      } else if (row.isFinish == 5) {
        //完成
        return;
      }
      if (row.id) {
        this.$router.push({
          path: path,
          query: { id: row.id }
        });
      }
    },
    print() {
      this.noprint = false;
      this.w = this.wp;
      setTimeout(() => {
        this.$print(this.$refs.print);
      }, 500);
      setTimeout(() => {
        this.noprint = true;
        this.w = this.wf;
      }, 1500);
    },
    findList() {
      this.loading = true;
      var that = this;
      var resdata = {
        // cid: this.pm.cid,
        page: this.pageIndex,
        pageSize: this.pageSize
        // token: getToken()
      };
      // 调用后台接口
      request(`/item/select`, {
        method: "get",
        headers: {
          "Content-Type": "application/json"
        },
        params: resdata
      })
        .then(res => {
          that.loading = false;
          this.total = res.data.total;
          console.log(this.total);
          this.liebiao = res.data.object;
          console.log('liebiao:',this.liebiao);
        })
        .catch(e => {
          that.loading = false;
          console.log(e);
          this.$message.success("查询失败");
        });
    },
    // 编辑的操作
    handleEdit(index, row) {
      var that = this;
      that.loading = true;
      console.log(row);
      request(`/item/find/${row.id}`, {
        method: "get",
        headers: {
          "Content-Type": "application/json"
        },
        params: ""
      })
        .then(res => {
          var res = res.data.object;
          that.aa = true;
          that.ruleForm = res;
          let arr = [];
          console.log("res.personList:", res.personList);
          if (res.personList.length != 0) {
            res.personList.map((v, i) => {
              arr.push(v.id);
            });
            console.log("arr:", arr);
            console.log("that.ruleForm.personList:", that.ruleForm.personList);
          }
          that.arr = arr;
          that.loading = false;
          console.log(res.data.object);
        })
        .catch(e => {
          that.loading = false;
        });
    },
    // 删除的操作
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该分公司, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request(`/item/del/${row.id}`, {
            method: "get",
            headers: {
              "Content-Type": "application/json"
            },
            params: ""
          })
            .then(res => {
              console.log(res);
              this.findList();

              this.$message({
                type: "success",
                message: "删除成功!"
              });
              console.log("删除成功!并重新渲染首页信息");
            })
            .catch(e => {
              console.log(e);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 查询单独的分公司
    cxid() {
      var that = this;
      if (this.soltid == "") {
        this.findList();
      } else {
        request(`branch/find/${this.soltid}`, {
          method: "get",
          headers: {
            "Content-Type": "application/json",
            token: getToken()
          },
          params: ""
        }).then(res => {
          console.log(232323);
          console.log(res.data.object);
          this.tableData = res.data.object;
          this.$message({
            type: "success",
            message: "加载成功!"
          });
        });
      }
    },
    pageChange(pageIndex) {
      this.pageIndex = pageIndex;
      this.findList();
      console.log(pageIndex);
    },
    pageSizeChange(pageSize) {
      this.pageIndex = 1;
      this.pageSize = pageSize;
      this.findList();
    },
    // 编辑删除的操作

    // 详细的编辑过程
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // b编辑
          var that = this;
          request("/item/update", {
            method: "post",
            headers: { "Content-Type": "application/json" },
            data: this.ruleForm
          }).then(function(response) {
            that.aa = false;
            console.log(response);
            that.findList();
            that.$message({
              type: "success",
              message: "编辑成功!"
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.aa = false;
    }
  }
};
</script>

<style>
.sortable-ghost {
  opacity: 0.8;
  color: #fff !important;
  background: #42b983 !important;
}
.el-alert__content {
  width: 100%;
}
</style>

<style scoped>
.icon-star {
  margin-right: 2px;
}
.drag-handler {
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d {
  margin-top: 15px;
}
/* .el-alert__content {
  width: 50%;
} */
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
}
.el-alert__content {
  width: 100%;
}
</style>
